using System;
using System.Linq.Expressions;
using Web.App.Infrastructure.UnitOfWork.Contracts;
using NHibernate;
using NHibernate.Linq;
using System.Linq;

namespace Web.App.Infrastructure.UnitOfWork.Implementations
{
    public class NHibernateSession : IUoWSession
    {
        private readonly ISession session;

        public NHibernateSession(ISession session)
        {
            this.session = session;
        }

        public void Dispose()
        {
           session.Dispose();
        }

        public void Save<T>(T entity)
        {
            session.Save(entity);
        }

        public void SaveOrUpdate<T>(T entity)
        {
            session.SaveOrUpdate(entity);
        }

        public void Delete<T>(T entity)
        {
            session.Delete(entity);
        }

        public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate)
        {
            return session
                    .Linq<T>()
                    .Where(predicate);
        }

        public T LoadById<T>(object id)
        {
            return session.Load<T>(id);
        }

        public void Reconnect()
        {
            session.Reconnect();
        }

        public void Disconnect()
        {
            session.Disconnect();
        }


    }
}